package com.cgc999.host.activity;

import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.cgc999.host.R;
import com.cgc999.host.api.Collection;
import com.cgc999.host.config.Constant;
import com.trello.rxlifecycle3.components.support.RxAppCompatActivity;

import java.util.HashMap;
import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * 唤醒其他程序入口
 */
public class AwakenActivity extends RxAppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_awaken);
        LinearLayout start = findViewById(R.id.start_accessory);
//        start.setText(getResultMessage());
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                net();
//                startAccessory();
            }
        });
        findViewById(R.id.start_accessory_body).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setData(Uri.parse("csd://com.cgc999.accessory/cyn?type=110"));
//                intent.putExtra("", "");//这里Intent当然也可传递参数,但是一般情况下都会放到上面的URL中进行传递
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });
    }

    public void net() {
        String link = "https://www.jianshu.com/p/3625061c98ad";
        String title = "rxjava";
        String author = "唯吾德馨";

        Map<String, String> map = new HashMap();
        map.put("id", "1");
        map.put("link", link);
        map.put("title", title);
        map.put("author", author);

        Retrofit retrofit = new Retrofit.Builder().baseUrl(Constant.BaseUrl).build();
        Collection collection = retrofit.create(Collection.class);
        Map<String,String> login = new HashMap();
        login.put("username","username");
        login.put("password","password");
        Call<ResponseBody> call = collection.login(login);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                Log.e("onResponse", "请求成功信息: " + response.body().toString());

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.e("onFailure", "请求失败信息: " + t.getMessage());

            }
        });
//
        /*Retrofit retrofit = new Retrofit.Builder().baseUrl(Constant.BaseUrl)
                .addConverterFactory(GsonConverterFactory.create()) //对服务器数据进行解析
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) //RxJava适配器
                .build();
        IApiService iApiService = retrofit.create(IApiService.class);
        Call<Bean> bean = iApiService.getRetorfit();
        bean.enqueue(new Callback<Bean>() {
            @Override
            public void onResponse(Call<Bean> call, Response<Bean> response) {
                Log.e("onResponse", "请求成功信息: " + response.body().toString());
            }

            @Override
            public void onFailure(Call<Bean> call, Throwable t) {
                Log.e("onFailure", "请求失败信息: " + t.getMessage());

            }
        });*/

    }

    public void startAccessory() {
//        com.cgc999.accessory包路径
//        MainActivity:对应跳转的Activity类名
//        resUrl :String类型
//        picUrlList:数组，也可以是对象
        ComponentName componentName = new ComponentName(
                "com.cgc999.accessory",
                "com.cgc999.accessory.MainActivity");
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        String resurl = "startAccessorySuccess ";
        bundle.putString("resUrl", resurl);
        String[] picurllist = {"百度", "阿里", "腾讯"};
        bundle.putSerializable("picUrlList", picurllist);
        intent.putExtras(bundle);
        intent.setComponent(componentName);
        startActivity(intent);
    }

    public String getResultMessage() {
        //        应用程序安装后，按以上方式可进行调用.
//                接受activity:
        Intent mIntent = this.getIntent();
        String resUrl = mIntent.getStringExtra("resUrl");
        String[] picUrlList = (String[]) mIntent.getSerializableExtra("picUrlList");
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(resUrl);
        if (null != picUrlList) {
            for (int i = 0; i < picUrlList.length; i++) {
                stringBuffer.append(picUrlList[i]);
            }
        }
        return stringBuffer.toString();
    }
}
